package com.slipper.ai.model.embedding.support;

import com.slipper.ai.model.embedding.EmbeddingModelBuilder;
import com.slipper.core.gson.GsonUtil;
import com.slipper.exception.CommonException;
import com.slipper.system.ai.bo.AiModelBO;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.zhipuai.ZhiPuAiEmbeddingModel;
import org.springframework.ai.zhipuai.ZhiPuAiEmbeddingOptions;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi;

import java.util.Map;

public class ZhipuaiEmbeddingModelBuilder implements EmbeddingModelBuilder {
    @Override
    public EmbeddingModel build(AiModelBO model) {
        Map<String, Object> options = GsonUtil.toMap(model.getParamJson());
        if (options == null
                || options.isEmpty()
                || !options.containsKey("apiUrl")
                || !options.containsKey("apiKey")) {
            throw new CommonException("参数配置不正确！");
        }

        String apiUrl = (String) options.get("apiUrl");
        String apiKey = (String) options.get("apiKey");
        return new ZhiPuAiEmbeddingModel(
                new ZhiPuAiApi(apiUrl, apiKey),
                MetadataMode.EMBED,
                ZhiPuAiEmbeddingOptions.builder()
                        .model(model.getSupplierModelName())
                        .dimensions(model.getSupplierModelDimension())
                        .build());
    }
}
